using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//Assegurar que não faltarão os componentes abaixo.
[RequireComponent(typeof(CharacterController))]

public class Player_Movement : MonoBehaviour
{
	private const string TAG = "Player";
	private float speed = 10.0f;
	private float timeToMove = 0.0f;
	private bool isGrounded = false;
	
	private Vector3 gravity = Vector3.zero;
	private Vector3 move = Vector3.zero;
	private CharacterController control;
	
	public Enum_Gravity enum_Gravity;
	
	
	void Start ()
	{
		control = GetComponent<CharacterController> ();
		primaryGravity ();
	}
	
	void FixedUpdate ()
	{
		Movement();
	}
	
	void OnControllerColliderHit(ControllerColliderHit hit)
	{
		if (hit.transform.tag.Equals(Tags.platform_Broken))
		{
			hit.gameObject.SendMessage("RecieveMessage");
		}
	}
	
	#region SETTERS_AND_GETTERS
	
	public Vector3 Gravity
	{
		get{ return gravity;}
	}
	
	public float TimeToMove { set { timeToMove = value; } }
	
	public Enum_Gravity EnumGravity
	{
		get{ return enum_Gravity; }
		set{ enum_Gravity = value; }
	}
	
	public void setGravity (Vector3 g)
	{
		this.gravity = g;
	}
	
	public void setIsGrounded (bool isGrounded)
	{
		this.isGrounded = isGrounded;
	}
	
	public void setPosition (Vector3 pos)
	{
		transform.position = pos;
	}
	
	public void setRotation (Vector3 rot)
	{
		transform.rotation = Quaternion.Euler (rot);
	}
	#endregion
	
	private void Movement ()
	{
		//Nao se mover durante um tempo.
		timeToMove += Time.deltaTime;
		
		if (timeToMove > 1.2f)
		{
			if (Enum_Gravity.LEFT == enum_Gravity || Enum_Gravity.RIGHT == enum_Gravity) {
				move = new Vector3 (0f, Input.GetAxisRaw ("Vertical"), 0f);
				move.Normalize();
				move *= speed;
				
			} else if (Enum_Gravity.UP == enum_Gravity || Enum_Gravity.DOWN == enum_Gravity) {
				move = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0f, 0f);
				move.Normalize();
				move *= speed;
			}
			
			//Velocidade é zerada, a gravidade se inverte e o jogador não pode trocar
			//trocar a gravidade até encostar novamente no chão.
			if (Input.GetKeyDown (KeyCode.Space) && isGrounded) {
				gravity = Vector3.zero;
				Physics.gravity *= -1;
				setIsGrounded (false);

				//Altera o sentido da gravidade para o inverso (UP - DOWN, LEFT - RIGHT)
				changeEnumGravity ();
			}
		
			//Adicionar força se o jogador não estiver no chão
			if (!isGrounded) {
				gravity += Physics.gravity * Time.deltaTime;
			}

            print(Physics.gravity);
			
			move += (gravity * speed);
			control.Move (move * Time.deltaTime);
		}
	}
	
	/// <summary>
	/// Altera a gravidade do jogador para o inverso e rotaciona o mesmo.
	/// </summary>
	private void changeEnumGravity ()
	{
		switch (enum_Gravity) {
		case Enum_Gravity.UP:
			enum_Gravity = Enum_Gravity.DOWN;
			transform.rotation = Quaternion.Euler (0, 0, 0f);
			break;
			
		case Enum_Gravity.DOWN:
			enum_Gravity = Enum_Gravity.UP;
			transform.rotation = Quaternion.Euler (0, 0, 180f);
			break;
			
		case Enum_Gravity.LEFT:
			enum_Gravity = Enum_Gravity.RIGHT;
			transform.rotation = Quaternion.Euler (0, 0, 90f);
			break;
			
		case Enum_Gravity.RIGHT:
			enum_Gravity = Enum_Gravity.LEFT;
			transform.rotation = Quaternion.Euler (0, 0, 270f);
			break;
		
		}
	}
	
	/// <summary>
	/// Gravidade inicial do jogo que será mudada dependendo do Enum inicial.
	/// </summary>
	private void primaryGravity ()
	{
		switch (enum_Gravity) {
		case Enum_Gravity.DOWN:
			Physics.gravity = new Vector3 (0f, -9.8f, 0f);
			transform.rotation = Quaternion.Euler (0, 0, 0f);
			break;
			
		case Enum_Gravity.UP:
			Physics.gravity = new Vector3 (0f, 9.8f, 0f);
			transform.rotation = Quaternion.Euler (0, 0, 180f);
			break;
			
		case Enum_Gravity.LEFT:
			Physics.gravity = new Vector3 (-9.8f, 0f, 0f);
			transform.rotation = Quaternion.Euler (0, 0, 270f);
			break;
			
		case Enum_Gravity.RIGHT:
			Physics.gravity = new Vector3 (9.8f, 0f, 0f);
			transform.rotation = Quaternion.Euler (0, 0, 90f);
			break;
		}
	}
}

public enum Enum_Gravity
{
	UP,
	DOWN,
	LEFT,
	RIGHT
}